package com.gxa.modules.pc.hospital.controller;

import com.gxa.common.exception.ResultException;
import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.model.dto.PackageDto;
import com.gxa.modules.model.dto.PackageForChooseDto;
import com.gxa.modules.model.dto.PackageTypeDetailDto;
import com.gxa.modules.model.dto.PackageTypeDto;
import com.gxa.modules.model.entity.hospital.HospitalPark;
import com.gxa.modules.model.entity.hospital.Items;
import com.gxa.modules.model.entity.hospital.Package;
import com.gxa.modules.model.vo.PackageVo;
import com.gxa.modules.oss.factory.OSSFactory;
import com.gxa.modules.pc.hospital.mapper.PackageMapper;
import com.gxa.modules.pc.hospital.service.PackageService;
import com.gxa.modules.pc.hospital.service.PackageTypeService;
import com.gxa.modules.pc.hospital.service.impl.PackageTypeServiceImpl;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 套餐信息 前端控制器
 * </p>
 *
 * @author gxa
 * @since 2022-11-12 05:04:59
 */
@Api(tags = "体检套餐")
@RestController
@RequestMapping("/pc/package")
public class PackageController {

    @Autowired
    private PackageService packageService;

    @Autowired
    private PackageTypeService packageTypeService;

    @ApiOperation(value = "带条件的分页查询")
    @PostMapping("/package")
    public Result<Map> queryByPage (@RequestBody PackageVo packageVo){
        Map<String, Object> stringObjectMap = this.packageService.queryAll(packageVo);
        return new Result<Map>().ok(stringObjectMap);
    }

    @ApiOperation(value = "根据套餐类型id查询")
    @ApiImplicitParam(paramType = "path",name = "id",value ="套餐类型id",dataType ="Integer")
    @PostMapping("/packageById/{id}")
    public Result<Package> queryById (@PathVariable("id") Integer id){
        try {
            Package aPackage = this.packageService.queryById(id);
            return new Result<Package>().ok(aPackage);
        }catch (Exception e){
            e.printStackTrace();
            return new Result<Package>().error("查询失败");
        }
    }

    @ApiOperation(value = "根据套餐类型id删除")
    @ApiImplicitParam(paramType = "body", name = "ids", value = "删除的id集合", dataType = "list")
    @DeleteMapping("/packageById")
    public Result deleteById (@RequestBody List<Long> ids ){
        try {
            this.packageService.delById(ids);
            return new Result().ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result().error("删除失败");
        }

    }

    @ApiOperation(value = "根据套餐类型id更改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "套餐类型id", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "state", value = "套餐类型状态", dataType = "Integer")
    })
    @PutMapping("/packageById")
    public Result updateStateByid (@RequestParam Integer id,@RequestParam Integer state){
        try {
            this.packageService.updateStateById(id, state);
            return new Result().ok("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result<>().error("修改失败");
        }
    }


    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(paramType = "body",name = "ids",value ="要删除数据的id集合",dataType ="List")
    @DeleteMapping
    public Result delByIds (@RequestBody List<Long> ids){
        String s = packageService.delByIds(ids);

        return new Result().ok(s);
    }

    @ApiOperation(value = "图片上传")
    @PostMapping("/up")
    public Result<String> upload(@RequestParam("file") MultipartFile file)throws Exception {
        if (file.isEmpty()) {
            throw new ResultException("上传文件不能为空");
        }

        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        return new Result<String>().ok(url);
    }

    @ApiOperation(value = "新增套餐")
    @PostMapping("/add")
    public Result add(@RequestBody Package hospitalPackage){
        packageService.add(hospitalPackage);
        return new Result().ok();
    }

    @ApiOperation(value = "套餐类型下拉框")
    @GetMapping("/TypeList")
    public Result queryType(){
        List<PackageTypeDto> packageTypeDtos = this.packageTypeService.queryList();
        return new Result().ok(packageTypeDtos);
    }
    @ApiOperation(value = "医院园区下拉框")
    @GetMapping("/ParkList")
    public Result queryPark(){
        List<HospitalPark> hospitalParks = this.packageTypeService.selectParkList();
        return new Result().ok(hospitalParks);
    }
    @ApiOperation(value = "项目下拉框")
    @GetMapping("/ItemsList")
    public Result queryItems(){
        List<Items> items = this.packageService.queryForChoose();
        return new Result().ok(items);
    }
    @ApiOperation(value = "套餐更新")
    @PutMapping("/updatePackage")
    public Result updatePackage(Package apackage){
        try {
            this.packageService.update(apackage);
            return new Result().ok("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result().ok("更新失败");
        }
    }



//    @ApiOperation(value = "更新套餐类型详细信息")
//    @ApiImplicitParam(paramType = "query",name = "id",value ="套餐类型id",dataType ="Integer")
//    @PutMapping("/updateById")
//    public Result updateById (@RequestBody PackageTypeDto packageTypeDto ){
//        try {
//            this.packageService.updateStateById(packageTypeDto);
//            return new Result().ok("修改成功");
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result().error("修改失败");
//        }
//
//    }
//    @ApiOperation(value = "增加套餐类型")
//    @PostMapping("/add")
//    public Result add (@RequestBody PackageTypeDetailDto packageTypeDetailDto ){
//        try {
//            this.packageTypeService.add(packageTypeDetailDto);
//            return new Result().ok("添加成功");
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result().error("添加失败");
//        }
//
//    }

}
